package com.xie.service;

import com.github.pagehelper.PageHelper;
import com.xie.mapper.UserMapper;
import com.xie.pojo.Role;
import com.xie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    JavaMailSender mailSender;

    public void addUser(User user){
        userMapper.insert(user);
    }

    public User findUserByName(String username){
        User user=new User();
        user.setName(username);
        User user1 = userMapper.selectOne(user);
        return user1;
    }
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User userInfo=userMapper.findByUserName(s);
        if (null == userInfo){
            throw new UsernameNotFoundException(s);
        }
        return new org.springframework.security.core.userdetails.User(userInfo.getName(),userInfo.getPassword(),getAuthority(userInfo.getRoles()));
    }

    private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
        List<SimpleGrantedAuthority> authoritys = new ArrayList();
        for (Role role : roles) {
            authoritys.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return authoritys;
    }

    public void update(User userByName) {
        userMapper.updateByPrimaryKey(userByName);
    }

    public void addMoney(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        user.setMoney(user.getMoney()+300);
        userMapper.updateByPrimaryKey(user);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("981800006@qq.com");
        message.setTo(user.getEmail());
        message.setSubject("哎呀,谢谢");
        message.setText("你的资源通过啦,当前余额:"+user.getMoney());
        mailSender.send(message);
    }

    public void nopass(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("981800006@qq.com");
        message.setTo(user.getEmail());
        message.setSubject("哎呀,失败了");
        message.setText("你的资源未通过,可能是重复资源或者失效资源!");
        mailSender.send(message);
    }

    public int countALl() {
        return userMapper.selectCount(null);
    }

    public List<User> findAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return userMapper.selectAll();
    }

    public int findZyById(Integer id) {
        return userMapper.findZyByIdCount(id);
    }

    public int findTjZyByUser(String name){
        return userMapper.findZyByUserCount(name);
    }

    public void updateMoney(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    public boolean checkWap(Integer id, Integer uid) {
        if(userMapper.checkWap(id,uid)>0){
            return true;
        }else {
            return false;
        }
    }

    public void addUserZy(Integer uid, Integer zid) {
        userMapper.insertUserZy(uid,zid);
    }
}
